#region Using
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CY.GFive.Core.Business;
using CY.Utility.DBUtility;
#endregion Using

namespace CY.GFive.DALProviders.SqlServerProvider
{
	public class ClassRoomProvider : CY.GFive.Core.Providers.DALProvider.IClassRoomProvider
	{
		#region Sql
		private static readonly string SqlSelectClassRoom = "SELECT [Id], [Code],[Name],[RoomCate],[AvailNum],[RoomState] FROM [ClassRoom] WHERE  [Id]=@Id";

		private static readonly string SqlInsertClassRoom = "INSERT INTO [ClassRoom]([Code],[Name],[RoomCate],[AvailNum],[RoomState]) values(@Code,@Name,@RoomCate,@AvailNum,@RoomState);SELECT SCOPE_IDENTITY();";

		private static readonly string SqlUpdateClassRoom = "UPDATE [ClassRoom] SET [Code] = @Code,[Name] = @Name,[RoomCate] = @RoomCate,[AvailNum] = @AvailNum,[RoomState] = @RoomState WHERE  [Id]=@Id";

		private static readonly string SqlDeleteClassRoom = "DELETE [ClassRoom] WHERE  [Id]=@Id";

		private static readonly string SqlGetAllClassRoom = "SELECT [Id], [Code],[Name],[RoomCate],[AvailNum],[RoomState] FROM [ClassRoom]";
		#endregion

		#region IClassRoomProvider Members
		public Core.Business.ClassRoom Select(int id)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, id);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlSelectClassRoom);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				Core.Business.ClassRoom classRoom = new Core.Business.ClassRoom();

				if (!reader.IsDBNull(0)) classRoom.Id = reader.GetInt32(0);
				if (!reader.IsDBNull(1)) classRoom.Code = reader.GetString(1);
				if (!reader.IsDBNull(2)) classRoom.Name = reader.GetString(2);
				if (!reader.IsDBNull(3)) classRoom.RoomCate = reader.GetString(3);
				if (!reader.IsDBNull(4)) classRoom.AvailNum = reader.GetInt32(4);
				if (!reader.IsDBNull(5)) classRoom.RoomState = reader.GetString(5);

				return classRoom;
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();

				return null;
			}
		}

		public void Insert(Core.Business.ClassRoom classRoom)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Code", SqlDbType.NVarChar, classRoom.Code);
			sql.AddParameter("@Name", SqlDbType.NVarChar, classRoom.Name);
			sql.AddParameter("@RoomCate", SqlDbType.NVarChar, classRoom.RoomCate);
			sql.AddParameter("@AvailNum", SqlDbType.Int, classRoom.AvailNum);
			sql.AddParameter("@RoomState", SqlDbType.NVarChar, classRoom.RoomState);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlInsertClassRoom);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				if (!reader.IsDBNull(0)) classRoom.Id = int.Parse(reader.GetDecimal(0).ToString());

				reader.Close();
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}

		public void Update(Core.Business.ClassRoom classRoom)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Code", SqlDbType.NVarChar, classRoom.Code);
			sql.AddParameter("@Name", SqlDbType.NVarChar, classRoom.Name);
			sql.AddParameter("@RoomCate", SqlDbType.NVarChar, classRoom.RoomCate);
			sql.AddParameter("@AvailNum", SqlDbType.Int, classRoom.AvailNum);
			sql.AddParameter("@RoomState", SqlDbType.NVarChar, classRoom.RoomState);
			sql.AddParameter("@Id", SqlDbType.Int, classRoom.Id);
			sql.ExecuteSql(SqlUpdateClassRoom);
		}

		public void Delete(Core.Business.ClassRoom classRoom)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, classRoom.Id);
			sql.ExecuteSql(SqlDeleteClassRoom);
		}

		public IList<Core.Business.ClassRoom> GetAllClassRoom()
		{
			IList<Core.Business.ClassRoom> classRoomlist = new List<Core.Business.ClassRoom>();
			SqlServerUtility sql = new SqlServerUtility();

			SqlDataReader reader = sql.ExecuteSqlReader(SqlGetAllClassRoom);

			if(reader != null)
			{
				while(reader.Read())
				{
					Core.Business.ClassRoom classRoom = new Core.Business.ClassRoom();

					if (!reader.IsDBNull(0)) classRoom.Id = reader.GetInt32(0);
					if (!reader.IsDBNull(1)) classRoom.Code = reader.GetString(1);
					if (!reader.IsDBNull(2)) classRoom.Name = reader.GetString(2);
					if (!reader.IsDBNull(3)) classRoom.RoomCate = reader.GetString(3);
					if (!reader.IsDBNull(4)) classRoom.AvailNum = reader.GetInt32(4);
					if (!reader.IsDBNull(5)) classRoom.RoomState = reader.GetString(5);

					classRoom.MarkOld();
					classRoomlist.Add(classRoom);
				}
				reader.Close();
			}
			return classRoomlist;
		}
		#endregion
	}
}
